/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.admin.bulletin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fuyao.cloud.admin.bulletin.Bulletin;
import com.fuyao.cloud.admin.bulletin.enums.BulletinStatusEnum;
import com.fuyao.cloud.admin.bulletin.mapper.BulletinMapper;
import com.fuyao.cloud.admin.bulletin.service.BulletinReadrecordService;
import com.fuyao.cloud.admin.bulletin.service.BulletinService;
import com.fuyao.cloud.admin.bulletin.vo.BulletinVO;
import com.fuyao.cloud.common.biz.service.FyBaseServiceImpl;
import com.fuyao.cloud.common.core.constant.CommonConstants;
import com.fuyao.cloud.common.core.model.BaseEntity;
import com.fuyao.cloud.common.core.util.R;
import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 通知公告
 *
 * @author lyhills
 * @date 2023-06-29 17:38:50
 */
@Service
@AllArgsConstructor
public class BulletinServiceImpl extends FyBaseServiceImpl<BulletinMapper, Bulletin> implements BulletinService {

    private final BulletinReadrecordService bulletinReadrecordService;

    @Override
    public Bulletin create() {
        Bulletin bulletin = new Bulletin();
        bulletin.setUnid(UUID.randomUUID().toString());
        bulletin.setStatus("0");
        return bulletin;
    }

    @Transactional
    @Override
    public R batchDelete(List<Long> ids) {
        LambdaQueryWrapper<Bulletin> eq = Wrappers.<Bulletin>query().lambda().eq(Bulletin::getStatus, 1).in(BaseEntity::getId, ids);
        long count = this.count(eq);
        Assert.isTrue(count <= 0, "只能删除草稿状态数据，所选删除数据包含已发布的信息，请重新选择！");
        boolean b = this.removeBatchByIds(ids);
        return R.ok(b, String.format("成功删除%s条数据", ids.size()));
    }

    @Transactional
    @Override
    public R pushBulletin(List<Long> ids) {
        LambdaQueryWrapper<Bulletin> eq = Wrappers.<Bulletin>query().lambda().in(BaseEntity::getId, ids);
        List<Bulletin> bulletinList = this.list(eq);
        if (CollUtil.isNotEmpty(bulletinList)) {
            for (Bulletin bulletin : bulletinList) {
                bulletin.setPushTime(LocalDateTimeUtil.now());
                bulletin.setPushUser(getFyUser());
                bulletin.setStatus(BulletinStatusEnum.PUSH.getStatus());
                this.updateById(bulletin);
            }
        }
        return R.ok(CommonConstants.SUCCESS, "发布成功");
    }

    @Override
    public List<BulletinVO> getBulletinList() {
        LambdaQueryWrapper<Bulletin> queryWrapper = Wrappers.<Bulletin>query().lambda().eq(Bulletin::getStatus, BulletinStatusEnum.PUSH.getStatus()).orderByDesc(Bulletin::getPushTime);
        List<Bulletin> list = this.list(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            return list.stream().map(bulletin -> {
                BulletinVO bulletinVO = new BulletinVO();
                BeanUtil.copyProperties(bulletin, bulletinVO);
                bulletinVO.setCurUserReadCount(bulletinReadrecordService.countRead(bulletin.getId(), getFyUser().getId()));
                return bulletinVO;
            }).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }
}
